/*
* @Author: Administrator
* @Date:   2018-01-03 11:01:09
* @Last Modified by:   Administrator
* @Last Modified time: 2018-01-03 12:40:33
*/
import React from 'react'
import PropTypes from 'prop-types'

// 按需加载模块
export default class Bundle extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      mod: null,
    };
  }

  componentWillMount() {
    this.load(this.props)
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.load !== this.props.load) {
      this.load(nextProps)
    }
  }

  load(props) {
    this.setState({
      mod: null,
    });
    // 注意这里，使用Promise对象; mod.default导出默认
    props.load().then((mod) => {
      this.setState({
        mod: mod.default ? mod.default : mod,
      });
    });
  }

  render() {
    return this.state.mod ? this.props.children(this.state.mod) : null;
  }
}
Bundle.propTypes = {
  load: PropTypes.func,
  children: PropTypes.func,
}
